jQ(function(){
	jQ("div.my_clips_menu a.clipmanage").addClass("clipmanageonit");
	jQ("#left_clip_list").empty();
	jQ("input.hminput").val(0);
	analyzeUrl();
//	initLeftCalendar();
//	loadTodayEvents();
	loadMainpartRecords();
	jQ("#viewopt").dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		closeOnEscape:false,
		draggable:false,
		height:200,
		buttons: {
			
			"确定": function() {
				var opt=jQ("#viewopt");
				var triggerfun=opt.data("function");
				opt.trigger(triggerfun);
				opt.removeData("function");
				jQ("#viewopt_p").hide();
				jQ("#viewopt_img").show();
				activeBtnOnDialog("viewopt",false);
				//jQ(this).dialog('close');
			},
			"取消": function() {
				var viewopt=jQ("#viewopt");
				var triggerfun=jQ("#viewopt").data("function");
				viewopt.unbind(triggerfun);
				viewopt.find("input.eventid").val("");
				viewopt.removeData("function");
				jQ(this).dialog('close');
			}
		},
		close: function() {
			jQ("#viewopt_p").show();
			jQ("#viewopt_img").hide();
			activeBtnOnDialog("viewopt",true);
		},
		open:function(){
		}
	});
});

function analyzeUrl(){
//	var href="";
//	var paramStr=location.href;
//	if(paramStr.indexOf("#")>0){
//		href=paramStr.substring(paramStr.indexOf("#"));
//    }
//	if(href.length>0){
//		jQ("div.samepage a").each(function(){
//			var classname=jQ(this).attr("onclass");
//			jQ(this).removeClass(classname).removeClass("onit");
//		});
//		var obj=jQ("div.samepage a[href='"+href+"']");
//		obj.addClass(obj.attr("onclass")).addClass("onit");
//	}else{
//		jQ("div.samepage a[href='#all']").addClass("allonit").addClass("onit");
//	}
//	var currentpage=eval(getParameter("currentpage"));
//	if(isNaN(currentpage)||currentpage==null){
//		jQ("#mainpart_list_content").data("currentpage",0);
//	}else{
//		jQ("#mainpart_list_content").data("currentpage",currentpage);
//	}
	var searchcontent=getParameter("content");
	if(searchcontent==null||searchcontent.length==0||typeof(searchcontent)=='undefined'){
//		window.location.href=jQ("#ctxVal").val()+"/myclip/clip_list.html";
//		return;
		searchcontent="";
		jQ("#allownshareinvite_title").hide();
		jQ("#clipsearch_welcome").show();
	}else{
		jQ("#clipsearch_welcome").hide();
		jQ("#allownshareinvite_title").show();
	}
	jQ("#mainpart_list_content").data("currentpage",0);
	jQ("#search_content").val(decodeURI(searchcontent));
}

function setHeightKeyWord(content, keyword) {
	if (keyword == "")
	return;
	var tempHTML = content;
	var htmlReg = new RegExp("\<.*?\>", "i");
	var arrA = new Array();
	for (var i = 0; true; i++) {
	var m = htmlReg.exec(tempHTML);
	if (m) {
	arrA[i] = m;
	}
	else {
	break;
	}
	tempHTML = tempHTML.replace(m, "[[[[" + i + "]]]]");
	}
	var replaceText;
	replaceText = "<strong>$1</strong>";
	var arrayWord = keyword.split(',');
	for (var w = 0; w < arrayWord.length; w++) {
	var r = new RegExp("(" + arrayWord[w].replace(/[(){}.+*?^$|\\\[\]]/g, "\\$&") + ")", "ig");
	tempHTML = tempHTML.replace(r, replaceText);
	}
	for (var i = 0; i < arrA.length; i++) {
	tempHTML = tempHTML.replace("[[[[" + i + "]]]]", arrA[i]);
	}
	return tempHTML;
}

var PAGESIZE=15;
function loadMainpartRecords(){
	var currentpage=eval(jQ("#mainpart_list_content").data("currentpage"));
	var content=jQ("#search_content").val();
	if(content==null||content.length==0){
		jQ("#allownshareinvite_title").hide();
		jQ("#clipsearch_welcome").show();
		return;
	}else{
		jQ("#clipsearch_welcome").hide();
		jQ("#allownshareinvite_title").show();
	}
	jQ("#loadingui").show();
	jQ.ajax({
		type:"POST",
		url: "manageClipAjax!searchMainpartRecords.action",
		data:{
			currentpage:currentpage,
			content:content
		},
		success:function(data){
			currentpage=eval(data.currentpage);
			jQ("#mainpart_list_content").data("currentpage",currentpage);
			var totalcount=eval(data.totalcount);
			var pagecount=eval(data.pagecount);
			jQ("#mainpart_list_content").data("pagecount",pagecount);
			jQ("#cliplistrecordsstatus").empty().append("搜索包含内容\"<font> "+content+" </font>\"的档期，共 <font>"+totalcount+"</font> 条记录");
			
			var cliphtml="";
			for(var i=0;i<data.searchresults.length;i++){
				var eventobj=data.searchresults[i];
				var eventid=eventobj.eventid;
				var uid=eventobj.unameid;
				var uname=eventobj.uname;
				var userlogo=eventobj.unameslogo;
				var title=eventobj.title;//
				var reliedclip=eval(eventobj.reliedclip);
				var timesituation=eval(eventobj.timesituation);
				var cliptype=eval(eventobj.cliptype);
				var starttime=eval(eventobj.starttime);
				var endtime=eval(eventobj.endtime);
				var processstatus=eval(eventobj.processstatus);
				var iswholeday=eval(eventobj.iswholeday);
				var deadlinetime=eval(eventobj.deadlinetime);
				var createtime=eval(eventobj.createtime);
				var isexpire=eval(eventobj.isexpire);
				var clipstatus=eval(eventobj.clipstatus);
				var shareid=eventobj.shareid;
				var eventtype=eval(eventobj.eventtype);
				var seid=eval(eventobj.seid);
				var clipiscc=eval(eventobj.clipiscc);
				var isuserself=eval(eventobj.isuserself);
				title=setHeightKeyWord(title,content);
				cliphtml+="<tr id='search-"+eventid+"' cliptype="+cliptype+" shareid="+shareid+" isuserself="+isuserself+">";
				cliphtml+="<td class=\"content\">";
				cliphtml+="<div class=\"other\">";
				cliphtml+="<span class=\"head\" style=\"background:url('"+userlogo+"');\" title=\""+uname+"\"></span>";
				if(clipiscc){
					cliphtml+="<span class=\"v_icon\" title='档期认证'></span>";
				}else
					cliphtml+="<span class=\"space\" ></span>";
				cliphtml+="<span class=\"time\">";
				/*if(processstatus==-1){
					cliphtml+="<b class='clip_before' title='未开始'></b>";
				}else if(processstatus==0){
					cliphtml+="<b class='clip_ing' title='进行中'></b>";
				}else if(processstatus==1){
					cliphtml+="<b class=\"clip_over\" title='已结束'></b>";
				}else if(processstatus==2){
					cliphtml+="<b class=\"clip_stop\" title='已停止'></b>";
				}*/
				cliphtml+="<span class=\"timevalue\">";
				if(iswholeday){
					cliphtml+="<b >开始于</b>";
					cliphtml+="<b class=\"first\">"+formateTitleTime(starttime)+"</b>";
				}else{
					cliphtml+="<b class=\"first\">"+formateTitleTime(starttime)+"</b>";
					cliphtml+="<b class=\"second\">"+formateTitleTime(endtime)+"</b>";
				}
				cliphtml+="</span>";
				cliphtml+="</span>";
				cliphtml+="<span class=\"title\">";
				if(cliptype==3){
					if(eventtype==2&&(seid==0||seid==reliedclip)){
						cliphtml+="&nbsp;";
					}else
						cliphtml+="<i class='invite'></i>";
				}else if(cliptype==4){
					cliphtml+="<i class='request'></i>";
				}
				
				cliphtml+=title;
				cliphtml+="</span>";
				cliphtml+="<span class=\"createtime\" title='"+formateDetailTime(createtime)+"'>";
				cliphtml+=formatCreatetime(createtime);
				cliphtml+="</span>";
				cliphtml+="</div>";
				cliphtml+="</td>";
				cliphtml+="</tr>";
			}
			jQ("#mainpart_list_content").empty().append(cliphtml);
			jQ("#mainpart_list_content tr").click(function(event){
				var tar=event.target;
				if(jQ(tar).hasClass("noclick"))
					return;
				var cliptype=eval(jQ(this).attr("cliptype"));
				var shareid=jQ(this).attr("shareid");
				var id=jQ(this).attr("id");
				var isuserself=eval(jQ(this).attr("isuserself"));
				var eventid=eval(id.split("-")[1]);
				if(cliptype==3||cliptype==4){
					if(isuserself){
						window.open(jQ("#ctxVal").val()+"/myclip/clip_detail.html?eventid="+eventid);
					}else
						window.open(jQ("#ctxVal").val()+"/myclip/clip_detail.html?"+shareid);
				}else{
					window.open(jQ("#ctxVal").val()+"/myclip/clip_detail.html?eventid="+eventid);
				}
			});
			initPaging(currentpage,pagecount);
			jQ("#loadingui").hide();
		}
	});
}

function initPaging(currentpage,pagec){
	jQ("#pagingcontent").empty();
	jQ("#pagingcount").empty();
	var pagecount=pagec;
	if(pagecount==1){
		jQ("#pagingcontent").hide();
		jQ("#pagecount").hide();
		return;
	}else{
		var pagemin=currentpage-2;
		var pagemax=currentpage+2;
		if(pagemin<0){
			pagemin=0;
		}
		if(pagemax>pagecount-1){
			pagemax=pagecount-1;
		}
		var html="";
		html+="<a page='first'><span >&lt;&lt;</span></a>";
		for(var i=pagemin;i<=pagemax;i++){
			if(i==currentpage){
				html+="<b ><span>"+(i+1)+"</span></b>";
			}else
				html+="<a page='"+i+"'><span>"+(i+1)+"</span></a>";
		}
		html+="<a page='last'><span >&gt;&gt;</span></a>";
		jQ("#pagingcontent").append(html);
		jQ("#pagingcontent a").unbind("click").bind("click",function(){
			var page=jQ(this).attr("page");
			var currentpage=0;
			if(page=="first"){
				currentpage=0;
			}else if(page=="last"){
				currentpage=pagecount-1;
			}else{
				currentpage=eval(page);
			}
			jQ("#mainpart_list_content").data("currentpage",currentpage);
			loadMainpartRecords();
			//loadAllMemo(currentpage);
		});
		jQ("#pagingcontent").show();
		jQ("#pagingcount").append("共有"+pagecount+"页").show();
	}
}

function formateDetailTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy-MM-dd HH:mm");
}

function formateTitleTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy年MM月dd日");
}

function getTypeName(eng){
	if(eng=='other'){
		return "其他";
	}else if(eng=='work'){
		return "工作";
	}else if(eng=='socal'){
		return "社交";
	}else if(eng=='sect'){
		return "私人";
	}
}

function getParameter(name){
    var paramStr=location.search;
    if(paramStr.length==0)return null;
    if(paramStr.charAt(0)!='?')return null;
    paramStr=decodeURIComponent(paramStr);
    paramStr=paramStr.substring(1);
    if(paramStr.length==0)return null;
    var params=paramStr.split('&');
    var p = null;
    for(var i=0;i<params.length;i++){
        if(params[i].indexOf(name) >= 0){
            p = params[i].split('=');
            p = p[1];
        }
    }
    return p;
}